package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.LaborCostDetaiAdapter
import com.hinnotek.amoeba.databinding.ActivityLaborCostDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.bean.old.LaborCostDetailBean
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.PersonViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LaborCostDetailActivity : BaseActivity<ActivityLaborCostDetailBinding>() {

    private val viewModel by viewModels<PersonViewModel>()
    private lateinit var adapter: LaborCostDetaiAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("人工成本")

        setContentView(R.layout.activity_labor_cost_detail)
    }

    override fun initVew() {
        val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        bindingView.rlv.layoutManager = manager
        bindingView.rlv.adapter = LaborCostDetaiAdapter().also { adapter = it }
    }

    override fun setData() {
        val companyPersonCostId = intent.getIntExtra("companyPersonCostId", 0)
        getCompanyPersonCostPage(companyPersonCostId)
    }

    /**
     * app端获取全部成本记录分页接口
     */
    private fun getCompanyPersonCostPage(companyPersonCostId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.getCompanyPersonCostById(companyPersonCostId)
            .observe(this, object : ObserverState<LaborCostDetailBean>() {
                override fun onDataChange(data: LaborCostDetailBean) {
                    customDialog.dismiss()
                    if (data.companyPersonCostType == 1) {
                        bindingView.llLaborUnit.visibility = View.GONE
                        bindingView.tvLaborAmoebaType.text = "产品"
                        bindingView.tvLaborProject.text = data.provideProjectName
                    } else {
                        bindingView.tvLaborAmoebaType.text = "项目"
                        bindingView.llLaborUnit.visibility = View.VISIBLE
                        bindingView.tvLaborUnit.text = data.customerName
                        bindingView.tvLaborProject.text = data.projectName
                    }
                    adapter.setList(data.companyPersonList)
                    bindingView.tvLaborAmoeba.text = data.orgName
                    bindingView.tvLaborMoney.text = data.personMoney
                    super.onDataChange(data)
                }
                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

}